package com.unionftech.crm_android.ui

import android.annotation.SuppressLint
import android.os.Build
import android.os.Bundle
import android.view.KeyEvent
import androidx.appcompat.app.AppCompatActivity
import com.unionftech.common.common.GlobalInfoManager
import com.unionftech.common.common.language.DynamicLanguageLoader
import com.unionftech.common.common.language.LanguageDownloader
import com.unionftech.common.utils.LogUtils
import com.unionftech.common.utils.SpUtils
import com.unionftech.common.utils.StatusBarUtil
import com.unionftech.common.utils.Utils.getAppVersionCode
import com.unionftech.common.utils.Utils.setThemes
import com.unionftech.content_login.util.TokenManager
import com.unionftech.crm_android.AppConstants


@SuppressLint("CustomSplashScreen")
class UFSplashActivity : AppCompatActivity() {
    companion object {
        private const val TAG = "UFSplashActivity"
    }

    private var isDelayEnd = false
    private var isLoadingLanguageEnd = false

    override fun onCreate(savedInstanceState: Bundle?) {
        StatusBarUtil.fullScreen(this, true)
        initGlobalData()
        super.onCreate(savedInstanceState)
    }

    private fun initGlobalData() {
        val versionCode = SpUtils.getInstance().getInt(AppConstants.Key.KEY_CURRENT_VERSION)
        val appVersionCode = getAppVersionCode(this)
        if (appVersionCode > versionCode) {
            GlobalInfoManager.getInstance().updateIntroductionImage()
        } else {
            GlobalInfoManager.getInstance().updateAdImage()
        }
        val accessToken = TokenManager.getInstance().getAccessToken()
        GlobalInfoManager.getInstance().initUserToken(accessToken)
        GlobalInfoManager.getInstance().update()
    }

    override fun onResume() {
        super.onResume()
        //根据配置进行加载亮色/暗色模式
        setThemes(this)
        window.decorView.postDelayed({
            isDelayEnd = true
            enterApp()
        }, 250)
        loadLanguage {
            isLoadingLanguageEnd = true
            enterApp()
        }
    }

    private fun enterApp() {
        if (isDelayEnd && isLoadingLanguageEnd) {
            val versionCode = SpUtils.getInstance().getInt(AppConstants.Key.KEY_CURRENT_VERSION)
            val appVersionCode = getAppVersionCode(this)
            LogUtils.d(TAG, "versionCode--->$versionCode")
            LogUtils.d(TAG, "appVersionCode--->$appVersionCode")
            if (appVersionCode > versionCode) {
                SpUtils.getInstance().put(AppConstants.Key.KEY_CURRENT_VERSION, appVersionCode)
                gotoIntroduction()
            } else {
                gotoAdStart()
            }
            //取消界面跳转时的动画，使启动页的logo图片与注册、登录主页的logo图片完美衔接
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.UPSIDE_DOWN_CAKE) {
                overrideActivityTransition(OVERRIDE_TRANSITION_CLOSE, 0, 0)
            } else {
                overridePendingTransition(0, 0)
            }
            finish()
        }
    }

    private fun gotoIntroduction() {
        UFIntroductionActivity.startThis(this)
    }

    private fun gotoAdStart() {
        UFAdStartActivity.startThis(this)
    }

    private fun loadLanguage(callBack: (() -> Unit)) {
        val languageCode = DynamicLanguageLoader.getCurLanguageCode()
        LanguageDownloader.downloadLanguageJson(languageCode) {
            DynamicLanguageLoader.applyLanguage(this, languageCode)
            callBack.invoke()
        }
    }

    /**
     * 屏蔽物理返回键
     */
    override fun onKeyDown(keyCode: Int, event: KeyEvent?): Boolean {
        return if (keyCode == KeyEvent.KEYCODE_BACK) {
            true
        } else super.onKeyDown(keyCode, event)
    }

}